<?php
class ControllerReportInventory extends Controller
{
	private $error = array();
	
	public function brands()
	{
		$this->load->model("report/inventory");
		$this->load->model("category/productbrand");
		$this->load->model("common/control");
	
		
		//CB Brand
		$brand1 = $this->model_category_productbrand->getList();
		
		$brands = array();
		$brands[] = array("id" => "%", "name" => "All");
		foreach($brand1 as $item)
		{
			$brands[] = array("id" => $item['brandid'], "name" => $item['brandname']);
		}
		
		$this->data['cb_brand'] = $this->model_common_control->getDataCombobox($brands, "name", "id", $this->request->post['search_brand']);
		
		//Date
		$this->data['search_fromdate'] = $this->request->post['search_fromdate'];
		$this->data['search_todate'] = $this->request->post['search_todate']; 
		if($this->request->post['search_fromdate'] == "")
		{
			$this->data['search_fromdate'] = date("01/m/Y");
		}
		
		
		if($this->request->post['search_todate'] == "")
		{
			$this->data['search_todate'] = date("d/m/Y");
		}
		
		$search_fromdate = $this->date->formatMySQLDateQuery($this->data['search_fromdate'],"DMY","-");
		$search_todate = $this->date->formatMySQLDateQuery($this->data['search_todate'],"DMY","-");
		
		$brandid = $this->request->post['search_brand'];
		if($brandid == "") {
			$brandid = "%";
		}
		
		
		//$this->data['test'] = $this->model_report_inventory->getListByBrand("%","2011-09-12 00:00:00","2011-09-14 00:00:00");
		$this->data['reports'] = array();
		$this->data['data'] = $this->model_report_inventory->getListByBrand($brandid,$search_fromdate,$search_todate);
		
		for($i=0; $i<count($this->data['data']); $i++)
		{
			$this->data['data'][$i]['sumimportqty'] = (float)$this->data['data'][$i]['sumimportqty'];
			$this->data['data'][$i]['sumexportqty'] = (float)$this->data['data'][$i]['sumexportqty'];
			$this->data['data'][$i]['sumstartperiod'] = $this->model_report_inventory->getSumStartPeriod($this->data['data'][$i]['productid'], "2011-09-12 00:00:00" );
			$this->data['data'][$i]['sumendperiod'] = $this->data['data'][$i]['sumstartperiod'] + $this->data['data'][$i]['sumimportqty'] - $this->data['data'][$i]['sumexportqty'];
			$this->data['data'][$i]['totalcost'] = $this->data['data'][$i]['sumendperiod'] * $this->data['data'][$i]['importcost'] ;
			
			$this->data['reports'][$this->data['data'][$i]['brandid']]['brandid'] = $this->data['data'][$i]['brandid'];
			$this->data['reports'][$this->data['data'][$i]['brandid']]['brandname'] = $this->data['data'][$i]['brandname'];
			$this->data['reports'][$this->data['data'][$i]['brandid']]['items'][] = $this->data['data'][$i];
		}

		$this->id='content';
		
		if($this->request->post['display'] == "report")
		{
			$this->template="report/inventory_brands_print.tpl";
		}
		else
		{
			$this->template="report/inventory_brands.tpl";
		}
		
		$this->layout="layout/function";
		
		$this->render();
	}
	
	public function groups()
	{
		$this->load->model("report/inventory");
		$this->load->model("category/productbrand");
		$this->load->model("category/productgroup");
		$this->load->model("common/control");
		
		//CB Brand
		$brand1 = $this->model_category_productbrand->getList();
		
		$brands = array();
		//$brands[] = array("id" => "%", "name" => "All");
		foreach($brand1 as $item)
		{
			$brands[] = array("id" => $item['brandid'], "name" => $item['brandname']);
		}
		
		$this->data['cb_brand'] = $this->model_common_control->getDataCombobox($brands, "name", "id", $this->request->post['search_brand']);
		
		$brandid = $this->request->post['search_brand'];
		if($brandid == "") {
			$brandid = $brands[0]["id"];
		}
		
		
		//group
		$group1 = $this->model_category_productgroup->getList(" AND product_brand.brandid = '".$brandid."'");
		
		$groups = array();
		$groups[] = array("id" => "%", "name" => "All");
		foreach($group1 as $item)
		{
			$groups[] = array("id" => $item['groupid'], "name" => $item['groupname']);
		}
		
		$this->data['cb_group'] = $this->model_common_control->getDataCombobox($groups, "name", "id", $this->request->post['search_group']);
		
		$groupid = $this->request->post['search_group'];
		if($groupid == "") {
			$groupid = "%";
		}
		
		
		
		
		//Date
		$this->data['search_fromdate'] = $this->request->post['search_fromdate'];
		$this->data['search_todate'] = $this->request->post['search_todate']; 
		if($this->request->post['search_fromdate'] == "")
		{
			$this->data['search_fromdate'] = date("01/m/Y");
		}
		
		
		if($this->request->post['search_todate'] == "")
		{
			$this->data['search_todate'] = date("d/m/Y");
		}
		
		$search_fromdate = $this->date->formatMySQLDateQuery($this->data['search_fromdate'],"DMY","-");
		$search_todate = $this->date->formatMySQLDateQuery($this->data['search_todate'],"DMY","-");
		
		
		
		
		//$this->data['test'] = $this->model_report_inventory->getListByBrand("%","2011-09-12 00:00:00","2011-09-14 00:00:00");
		$this->data['reports'] = array();
		$this->data['data'] = $this->model_report_inventory->getListByGroup($brandid,$groupid,$search_fromdate,$search_todate);
		
		for($i=0; $i<count($this->data['data']); $i++)
		{
			$this->data['data'][$i]['sumimportqty'] = (float)$this->data['data'][$i]['sumimportqty'];
			$this->data['data'][$i]['sumexportqty'] = (float)$this->data['data'][$i]['sumexportqty'];
			$this->data['data'][$i]['sumstartperiod'] = $this->model_report_inventory->getSumStartPeriod($this->data['data'][$i]['productid'], "2011-09-12 00:00:00" );
			$this->data['data'][$i]['sumendperiod'] = $this->data['data'][$i]['sumstartperiod'] + $this->data['data'][$i]['sumimportqty'] - $this->data['data'][$i]['sumexportqty'];
			$this->data['data'][$i]['totalcost'] = $this->data['data'][$i]['sumendperiod'] * $this->data['data'][$i]['importcost'] ;
			
			$brandid = $this->data['data'][$i]['brandid'];
			$groupid = $this->data['data'][$i]['groupid'];
			$this->data['reports'][$brandid]['brandid'] = $this->data['data'][$i]['brandid'];
			$this->data['reports'][$brandid]['brandname'] = $this->data['data'][$i]['brandname'];
			$this->data['reports'][$brandid]['group'][$groupid]['groupid']= $this->data['data'][$i]['groupid'];
			$this->data['reports'][$brandid]['group'][$groupid]['groupname']= $this->data['data'][$i]['groupname'];
			$this->data['reports'][$brandid]['group'][$groupid]['items'][]= $this->data['data'][$i];
		}

		$this->id='content';
		
		if($this->request->post['display'] == "report")
		{
			$this->template="report/inventory_groups_print.tpl";
		}
		else
		{
			$this->template="report/inventory_groups.tpl";
		}
		
		$this->layout="layout/function";
		
		$this->render();
	}
	
	public function getcbgroup()
	{
		$this->load->model("category/productgroup");
		$this->load->model("common/control");
		//group
		$brandid = $this->request->post["search_brand"];
		$group1 = $this->model_category_productgroup->getList(" AND product_brand.brandid = '".$brandid."'");
		
		$groups = array();
		$groups[] = array("id" => "%", "name" => "All");
		foreach($group1 as $item)
		{
			$groups[] = array("id" => $item['groupid'], "name" => $item['groupname']);
		}
		
		$this->data['output'] = $this->model_common_control->getDataCombobox($groups, "name", "id", $this->request->post['search_group']);
		
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
		
	}
	
	public function stocks()
	{
		$this->load->model("report/inventory");
		$this->load->model("category/stock");
		$this->load->model("common/control");
		
		//stock
		$stockid = $this->request->post['search_stock'];
		$stock1 = $this->model_category_stock->getList();
		
		$stocks = array();
		foreach($stock1 as $item)
		{
			$stocks[] = array("id" => $item['stockid'], "name" => $item['stockname']);
			if($item['stockid'] == $stockid)
			{
				$this->data['search_stockname'] = $item["stockname"];
			}
		}
		
		$this->data['cb_stock'] = $this->model_common_control->getDataCombobox($stocks, "name", "id", $this->request->post['search_stock']);
		
		
		if($stockid == "") {
			$stockid = $stocks[0]['id'];
			$this->data['search_stockname'] = $stocks[0]["stockname"];
		}
		
		
		
		
		//Date
		$this->data['search_fromdate'] = $this->request->post['search_fromdate'];
		$this->data['search_todate'] = $this->request->post['search_todate']; 
		if($this->request->post['search_fromdate'] == "")
		{
			$this->data['search_fromdate'] = date("01/m/Y");
		}
		
		
		if($this->request->post['search_todate'] == "")
		{
			$this->data['search_todate'] = date("d/m/Y");
		}
		
		$search_fromdate = $this->date->formatMySQLDateQuery($this->data['search_fromdate'],"DMY","-");
		$search_todate = $this->date->formatMySQLDateQuery($this->data['search_todate'],"DMY","-");
		
		
		
		
		//$this->data['test'] = $this->model_report_inventory->getListByBrand("%","2011-09-12 00:00:00","2011-09-14 00:00:00");
		$this->data['reports'] = array();
		$this->data['data'] = $this->model_report_inventory->getListByStock($stockid,$search_fromdate,$search_todate);
		
		for($i=0; $i<count($this->data['data']); $i++)
		{
			$this->data['data'][$i]['sumimportqty'] = (float)$this->data['data'][$i]['sumimportqty'];
			$this->data['data'][$i]['sumexportqty'] = (float)$this->data['data'][$i]['sumexportqty'];
			$this->data['data'][$i]['sumstartperiod'] = $this->model_report_inventory->getSumStartPeriod($this->data['data'][$i]['productid'], "2011-09-12 00:00:00" );
			$this->data['data'][$i]['sumendperiod'] = $this->data['data'][$i]['sumstartperiod'] + $this->data['data'][$i]['sumimportqty'] - $this->data['data'][$i]['sumexportqty'];
			$this->data['data'][$i]['totalcost'] = $this->data['data'][$i]['sumendperiod'] * $this->data['data'][$i]['importcost'] ;
			
			$brandid = $this->data['data'][$i]['brandid'];
			$groupid = $this->data['data'][$i]['groupid'];
			$this->data['reports'][$brandid]['brandid'] = $this->data['data'][$i]['brandid'];
			$this->data['reports'][$brandid]['brandname'] = $this->data['data'][$i]['brandname'];
			$this->data['reports'][$brandid]['group'][$groupid]['groupid']= $this->data['data'][$i]['groupid'];
			$this->data['reports'][$brandid]['group'][$groupid]['groupname']= $this->data['data'][$i]['groupname'];
			$this->data['reports'][$brandid]['group'][$groupid]['items'][]= $this->data['data'][$i];
		}

		$this->id='content';
		
		if($this->request->post['display'] == "report")
		{
			$this->template="report/inventory_stocks_print.tpl";
		}
		else
		{
			$this->template="report/inventory_stocks.tpl";
		}
		
		$this->layout="layout/function";
		
		$this->render();
	}
	
	public function outstock()
	{
		$this->load->model("report/inventory");
		$this->load->model("category/productbrand");
		$this->load->model("category/productgroup");
		$this->load->model("common/control");
		
		//CB Brand
		$brand1 = $this->model_category_productbrand->getList();
		
		$brands = array();
		//$brands[] = array("id" => "%", "name" => "All");
		foreach($brand1 as $item)
		{
			$brands[] = array("id" => $item['brandid'], "name" => $item['brandname']);
		}
		
		$this->data['cb_brand'] = $this->model_common_control->getDataCombobox($brands, "name", "id", $this->request->post['search_brand']);
		
		$brandid = $this->request->post['search_brand'];
		if($brandid == "") {
			$brandid = $brands[0]["id"];
		}
		
		
		//group
		$group1 = $this->model_category_productgroup->getList(" AND product_brand.brandid = '".$brandid."'");
		
		$groups = array();
		$groups[] = array("id" => "%", "name" => "All");
		foreach($group1 as $item)
		{
			$groups[] = array("id" => $item['groupid'], "name" => $item['groupname']);
		}
		
		$this->data['cb_group'] = $this->model_common_control->getDataCombobox($groups, "name", "id", $this->request->post['search_group']);
		
		$groupid = $this->request->post['search_group'];
		if($groupid == "") {
			$groupid = "%";
		}
		
		
		
		
		//Date
		$this->data['search_fromdate'] = $this->request->post['search_fromdate'];
		$this->data['search_todate'] = $this->request->post['search_todate']; 
		if($this->request->post['search_fromdate'] == "")
		{
			$this->data['search_fromdate'] = date("01/m/Y");
		}
		
		
		if($this->request->post['search_todate'] == "")
		{
			$this->data['search_todate'] = date("d/m/Y");
		}
		
		$search_fromdate = $this->date->formatMySQLDateQuery($this->data['search_fromdate'],"DMY","-");
		$search_todate = $this->date->formatMySQLDateQuery($this->data['search_todate'],"DMY","-");
		
		
		
		
		//$this->data['test'] = $this->model_report_inventory->getListByBrand("%","2011-09-12 00:00:00","2011-09-14 00:00:00");
		$this->data['reports'] = array();
		$this->data['data'] = $this->model_report_inventory->getListByOutStock($brandid,$groupid,$search_fromdate,$search_todate);
		
		for($i=0; $i<count($this->data['data']); $i++)
		{
			$this->data['data'][$i]['sumimportqty'] = (float)$this->data['data'][$i]['sumimportqty'];
			$this->data['data'][$i]['sumexportqty'] = (float)$this->data['data'][$i]['sumexportqty'];
			$this->data['data'][$i]['sumstartperiod'] = $this->model_report_inventory->getSumStartPeriod($this->data['data'][$i]['productid'], "2011-09-12 00:00:00" );
			$this->data['data'][$i]['sumendperiod'] = $this->data['data'][$i]['sumstartperiod'] + $this->data['data'][$i]['sumimportqty'] - $this->data['data'][$i]['sumexportqty'];
			$this->data['data'][$i]['totalcost'] = $this->data['data'][$i]['sumendperiod'] * $this->data['data'][$i]['importcost'] ;
			
			$brandid = $this->data['data'][$i]['brandid'];
			$groupid = $this->data['data'][$i]['groupid'];
			if($this->data['data'][$i]['sumendperiod']  <= $this->data['data'][$i]['notifyqty'] )
			{
				$this->data['reports'][$brandid]['brandid'] = $this->data['data'][$i]['brandid'];
				$this->data['reports'][$brandid]['brandname'] = $this->data['data'][$i]['brandname'];
				$this->data['reports'][$brandid]['group'][$groupid]['groupid']= $this->data['data'][$i]['groupid'];
				$this->data['reports'][$brandid]['group'][$groupid]['groupname']= $this->data['data'][$i]['groupname'];
				$this->data['reports'][$brandid]['group'][$groupid]['items'][]= $this->data['data'][$i];
			}
		}

		$this->id='content';
		
		if($this->request->post['display'] == "report")
		{
			$this->template="report/inventory_outstock_print.tpl";
		}
		else
		{
			$this->template="report/inventory_outstock.tpl";
		}
		
		$this->layout="layout/function";
		
		$this->render();
	}
	
	public function debtuser()
	{
		$this->load->model("report/service");
		
		$userid = $this->request->get['userid'];
		
		$users = $this->model_report_service->debtlist(" AND `partner`.userid = '".$userid."'");
		
		$this->data['user'] = $users[0];
		
		$this->load->model("voucher/voucher");
		$this->load->model("common/control");
		
		$this->data['accounttype'] = array();
		$vouchergroups_expense = array(
				0 => array("vouchergroupid" => "VEXSP", "vouchergroupname" => "Payments to suppliers for goods and services (Ex: Purchase Order)"),
				1 => array("vouchergroupid" => "VEXCR", "vouchergroupname" => "Payments to customers for goods and services (Ex: Return order)"),
				2 => array("vouchergroupid" => "VEXES", "vouchergroupname" => "Salary"),
				3 => array("vouchergroupid" => "VEXEA", "vouchergroupname" => "Amortization (loss of intangible asset value over time)"),
				4 => array("vouchergroupid" => "VEXEO", "vouchergroupname" => "Office Operating cost / Purchase Assets (Ex: Electronic, Water, Buy assets for company using)"),
				5 => array("vouchergroupid" => "VEXSL", "vouchergroupname" => "DEBT/LOANS of partners"),
				6 => array("vouchergroupid" => "VEXCL", "vouchergroupname" => "DEBT/LOANS of customers"),
				7 => array("vouchergroupid" => "VEXEL", "vouchergroupname" => "DEBT/LOANS of employee"),
				8 => array("vouchergroupid" => "VEXSR", "vouchergroupname" => "RENT ASSETS or others from partners"),
				9 => array("vouchergroupid" => "VEXEM", "vouchergroupname" => "Marketing cost, public relationship"),
				10 => array("vouchergroupid" => "VEXCT", "vouchergroupname" => "Return the deposit for customer when they receive products"),
				11 => array("vouchergroupid" => "VEXCC", "vouchergroupname" => "Change money to customer"),
				12 => array("vouchergroupid" => "VEXEA", "vouchergroupname" => "Capital")
			);
			
		$vouchergroups_income = array(
				0 => array("vouchergroupid" => "VINCS", "vouchergroupname" => "Receipts from the sale of goods or services (Ex: Sale Order, Sale services )"),
				1 => array("vouchergroupid" => "VINEI", "vouchergroupname" => "Receipts from internal company (employee)"),
				2 => array("vouchergroupid" => "VINSR", "vouchergroupname" => "Receipts from the returning products to supplier"),
				3 => array("vouchergroupid" => "VINSL", "vouchergroupname" => "DPBT/LOANS of Suppliers"),
				4 => array("vouchergroupid" => "VINCL", "vouchergroupname" => "DPBT/LOANS of Customer"),
				5 => array("vouchergroupid" => "VINEL", "vouchergroupname" => "DPBT/LOANS of Employee"),
				6 => array("vouchergroupid" => "VINCD", "vouchergroupname" => "Receipts from customer deposit order/products"),
				7 => array("vouchergroupid" => "VINCH", "vouchergroupname" => "Receipts from customer change products"),
				8 => array("vouchergroupid" => "VINCO", "vouchergroupname" => "Receipts from customer for other activities"),
				9 => array("vouchergroupid" => "VINSC", "vouchergroupname" => "Supplier change money"),
				10 => array("vouchergroupid" => "VINEA", "vouchergroupname" => "Capital")
			);
			
		foreach($vouchergroups_expense as $item)
		{
			$this->data['accounttype'][$item['vouchergroupid']] = $item['vouchergroupname']; 
		}
		
		foreach($vouchergroups_income as $item)
		{
			$this->data['accounttype'][$item['vouchergroupid']] = $item['vouchergroupname']; 
		}
		
		$this->data['data'] = $this->model_voucher_voucher->search(" AND `voucher`.partnerid = '".$userid."' AND `voucher`.grandtotal <> `voucher`.paymentamount ");
		
		
		$this->id='content';
		
		if($this->request->post['display'] == "report")
		{
			$this->template="report/debt_user_print.tpl";
		}
		else
		{
			$this->template="report/debt_user.tpl";
		}
		
		$this->layout="layout/function";
		
		$this->render();
	}
	
}
?>